package com.ycd.controller;

import com.ycd.result.JsonResult;
import com.ycd.service.IUserService;
import com.ycd.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody User user){
        if(user.getId()!=null){
            userService.updateById(user);
        }else{
            userService.insert(user);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        userService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(userService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(userService.selectList(null));
    }


    /**
     * 电影票查询：查询所有电影票
     */
    @GetMapping("/movies")
    public JsonResult movies(){
        return JsonResult.success(userService.movies());
    }

    /**
     * 电影票购买
     */
    @GetMapping("/buyTicket/{movieId}")
    public JsonResult buyTicket(@PathVariable("movieId") Long movieId){
        return JsonResult.success(userService.buyTicket(movieId));
    }

    /**
     * 电影观看
     */
    @GetMapping("/watchMovie/{movieId}")
    public JsonResult watchMovie(@PathVariable("movieId") Long movieId){
        return JsonResult.success(userService.watchMovie(movieId));
    }

}
